/*网络请求*/
import ENV_CONFIG from '@/config/env.js';
import ApiConstant from "@/config/ApiConstant";
// vuex 的使用  详情参考官网 https://uniapp.dcloud.io/vue-vuex

export default class Request {

    http(param) {
        // 请求参数
        var url = param.url,
            method = param.method,
            header = {
                'token':param.token || "",
            },
            data = param.data || {},
        showLoading = true;
        console.log(param)
        if (param.showLoad===false) {
            showLoading = false;
        }

        //拼接完整请求地址
        var requestUrl = ENV_CONFIG['wx_dev'].requestBaseUrl + url;

        //拼接完整请求地址（根据环境切换）
        //请求方式:GET或POST(POST需配置
        if (method) {
    //         if(process?.env && process.env?.NAME){
				// if(process?.env?.NAME === ''){}
				// data['programType'] = '微信小程序'
                
    //         }
            method = method.toUpperCase(); //小写改为大写
            if (method === "POST") {
                header = {"content-type":'application/json',...header}
            } else {
                header = {"content-type":'application/x-www-form-urlencoded',...header}
            }
        }

        //加载圈
        if (showLoading) {
            uni.showLoading({
                title: '加载中...'
            });
        }

        // 返回promise
        return new Promise((resolve, reject) => {
            // 请求
            uni.request({
                url: requestUrl,
                data: data,
                method: method,
                header: header,
                success: (res) => {
                    console.log('执行http请求')
                    let jsons= JSON.stringify(res)
                    //如果 nginx启动了，后端服务没有启动
                    if (jsons.includes('502 Bad Gateway')) {
                        //跳转到维护页面
                        wx.reLaunch({
                            url: '/pages/error/serviceDown',
                        })
                        return;
                    }


                    if (res.data && res.data.code === 401) {
                        try {
                            uni.clearStorageSync();
                        } catch (e) {}
                        //请登录
                        wx.reLaunch({
                            url: '/pages/tb/login',
                        })
                        return;
                    }
                    // 将结果抛出
                    resolve(res.data)
                },
                //请求失败
                fail: (e) => {
                    console.log('e:')
                    console.log(e)
                    if (e && e.errMsg === 'request:fail ') {
                        //服务未启动
                        //跳转到维护页面
                        wx.reLaunch({
                            url: '/pages/error/serviceDown',
                        })
                        return;
                    }else{
                        uni.showToast({
                            title: "" + e.data.msg,
                            icon: 'none'
                        });
                    }
                    resolve(e.data);
                },
                //请求完成
                complete() {
                    //隐藏加载
                    if (showLoading) {
                        uni.hideLoading();
                    }
                    resolve();
                    return;
                },

            })
        })
    }
    get(url,data,options={},header={}){
        options.method='get';
        options.data=data;
        options.url=url;
        options.token=uni.getStorageSync('token');
        return this.http(options);
    }
    get2(url,data,headers={},options={}){
        options.method='get';
        options.data=data;
        options.url=url;
        options.token=uni.getStorageSync('token');
        return this.http(options);
    }

    post(url,data,options={}){
        console.log('qvqsdd')
        options.method='post';
        options.data=data;
        options.url=url;
        options.token=uni.getStorageSync('token');
        return this.http(options);
    }
    post(url,data,showLoad,options={}){
        console.log('qvq')
        options.method='post';
        options.data=data;
        options.url=url;
        options.showLoad = showLoad;
        options.token=uni.getStorageSync('token');
        return this.http(options);
    }

    post(url,data,options={}){
        console.log('qweqwe')
        options.method='post';
        options.data=data;
        options.url=url;
        options.token=uni.getStorageSync('token');
        return this.http(options);
    }

    delete(url,data,options={}){
        options.method='delete';
        options.data=data;
        options.url=url;
        options.token=uni.getStorageSync('token');
        return this.http(options);
    }
}


